﻿using CarAppDto.Rule.RuleConfigDtos;
using CarAppService.Rule.IServices;
using D.UtilCore;
using D.UtilCore.Dtos;
using D.UtilCore.Filters;
using D.UtilSqlsugarOrm;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace CarAppAPI.Rule
{
    /// <summary>
    /// 规则组成
    /// </summary>
    [Route("/api/[controller]/[action]")]
    [Authorize]
    public class RuleConfigController : BaseApiController
    {
        private readonly IRuleConfigEntityService _ruleConfigService;
        public RuleConfigController(IRuleConfigEntityService ruleConfigService)
        {
            _ruleConfigService = ruleConfigService;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="reqAddRuleConfig"></param>
        /// <returns></returns>
        [HttpPost, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<int>> Add([FromBody] ReqAddRuleConfig reqAddRuleConfig)
        {
            // 验证和清理输入数据，防止注入攻击
            var sanitizedComment = System.Web.HttpUtility.HtmlEncode(reqAddRuleConfig.Descption);

            reqAddRuleConfig.CreateUser = AdminId;
            return await _ruleConfigService.AddAsync(reqAddRuleConfig);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{ids}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<int>> Delete(string ids)
        {
            var idsArray = ids.Split(",");
            return await _ruleConfigService.DeleteAsync(idsArray.ToList());
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="reqUpdateRuleConfig"></param>
        /// <returns></returns>
        [HttpPut, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<int>> Update( [FromBody] ReqUpdateRuleConfig reqUpdateRuleConfig)
        {
            reqUpdateRuleConfig.UpdateUser = AdminId;
            return await _ruleConfigService.UpdateAsync(reqUpdateRuleConfig);
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<RepRuleConfig>> Get(string? id)
        {
            if (string.IsNullOrEmpty(id))
            {
                id = AdminId;
            }
            return await _ruleConfigService.GetAsync(id);
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="reqGueryRuleConfigList"></param>
        /// <returns></returns>
        [HttpPost, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<List<RepRuleConfig>>> List([FromBody] ReqGueryRuleConfigList reqGueryRuleConfigList)
        {
            return await _ruleConfigService.QueryListAsync(reqGueryRuleConfigList);
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <returns></returns>
        [HttpPost, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<PageModel<RepRuleConfig>>> Pages([FromBody] ReqQueryRuleConfigPage reqQueryRuleConfigPage)
        {
            return await _ruleConfigService.QueryPagesAsync(reqQueryRuleConfigPage);
        }

        /// <summary>
        /// 根据VIN码获取规则
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{vin}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<List<RepRuleConfig>>> GetRuleConfigByVIN(string? vin)
        {         
            return await _ruleConfigService.GetRuleConfigByVINAsync(vin);
        }
    }
}
